/**
 * 
 */
package net.mikoo.seals.boss.common.helper;

import java.util.ArrayList;
import java.util.List;

import net.mikoo.seals.boss.model.Menu;


public class MenuHelper {
	
	public static List<Menu> convertToTree(List<Menu> childs, Long primaryId, int level) {
		
		List<Menu> menus = new ArrayList<Menu>();
		
		String str = "";
		int index = 0;
		for (Menu menu : childs) {
			
			if (menu.getParentId().equals(primaryId)) {
				index++;
				List<Menu> subs = loadByPrimaryId(childs, primaryId);
				for (int i = 0; i < level; i++) {
					
					if (i + 1 == level) {
						
						if (index == subs.size()) {
							str = str + "\u0008 \u0008 \u0008└─";
						} else {
							str = str + "\u0008 \u0008 \u0008├─";
						}
					} else {
						
						str = str + "\u0008 \u0008 \u0008│";
						
					}
					
				}
				menu.setMenuName(str + " " + menu.getMenuName());
				//menu.setMenuName(menu.getMenuName());
				menu.setChilds(subs);
				menu.setLevel(level);
				menus.add(menu);
				str = "";
				if (subs.size() > 0) {
					menus.addAll(convertToTree(childs, menu.getMenuId(), level + 1));
				}
			}
		}
		
		return menus;
		
	}
	
	
	public static List<Menu> convertToTree(List<Menu> menus, Long primaryId) {
		if(menus == null || menus.isEmpty()) {
			return null;
		}
		
		List<Menu> suMenus = loadByPrimaryId( menus,  primaryId) ;
		if(suMenus != null && !suMenus.isEmpty()) {
			
			for (Menu menu : suMenus) {
				
				List<Menu> childs =  convertToTree(menus, menu.getMenuId());
				menu.setChilds(childs);
			}
			
		}
		
		return suMenus;
	}
	
	/**
	 * 查询子类
	 * @param Menus
	 * @param primaryId
	 * @return
	 */
	public static List<Menu> loadByPrimaryId(List<Menu> menus, Long primaryId) {
		List<Menu> childs = new ArrayList<Menu>();
		
		for (Menu menu : menus) {
			if (menu.getParentId() == primaryId) {
				childs.add(menu);
			}
		}
		return childs;
	}
	
}
